package com.zhny.project.txt.gather.service.impl;

import com.zhny.common.utils.DateUtils;
import com.zhny.common.utils.security.ShiroUtils;
import com.zhny.common.utils.text.Convert;
import com.zhny.project.txt.dataset.domain.TxtDataset;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.gather.domain.TxtGather;
import com.zhny.project.txt.gather.mapper.TxtGatherMapper;
import com.zhny.project.txt.gather.service.ITxtGatherService;
import com.zhny.project.txt.indexset.domain.TxtIndexset;
import com.zhny.project.txt.indexset.mapper.TxtIndexsetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

/**
 * 汇总显示Service业务层处理
 *
 * @author yanqun
 * @date 2020-01-04
 */
@Service
public class TxtGatherServiceImpl implements ITxtGatherService
{
    @Autowired
    private TxtGatherMapper txtGatherMapper;
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;
    @Autowired
    private TxtIndexsetMapper txtIndexsetMapper;

    /**
     * 查询汇总显示
     *
     * @param gatherKey 汇总显示ID
     * @return 汇总显示
     */
    @Override
    public TxtGather selectTxtGatherById(String gatherKey)
    {
        return txtGatherMapper.selectTxtGatherById(gatherKey);
    }

    /**
     * 查询汇总显示列表
     *
     * @param txtGather 汇总显示
     * @return 汇总显示
     */
    @Override
    public List<TxtGather> selectTxtGatherList(TxtGather txtGather)
    {
        return txtGatherMapper.selectTxtGatherList(txtGather);
    }

    /**
     * 新增汇总显示
     *
     * @param txtGather 汇总显示
     * @return 结果
     */
    @Override
    public int insertTxtGather(TxtGather txtGather)
    {
        txtGather.setUpdateTime(DateUtils.getNowDate());
        txtGather.setUpdateBy(ShiroUtils.getUserId().toString());
        txtGather.setCreatTime(DateUtils.getNowDate());
        txtGather.setCreatBy(ShiroUtils.getUserId().toString());
        txtGather.setGatherKey(UUID.randomUUID().toString());
        txtGather.setDeleteFlag(0);
        return txtGatherMapper.insertTxtGather(txtGather);
    }

    /**
     * 修改汇总显示
     *
     * @param txtGather 汇总显示
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTxtGather(String datasetKey)
    {
        int result = 0;
        TxtDataset txtDataset = txtDatasetMapper.selectTableNameByDatasetKey(datasetKey);
        List<TxtIndexset> indexsetEnglistNameList = txtIndexsetMapper.seleEnglishNameByDatasetKey(datasetKey);
        ArrayList<String> qualityNameList = new ArrayList<>();
        //遍历EnglishName  列表 将名称取_后边的字符 并把截取去的字符 + "_quality" 拼成qualityName 字符   添加到 qualityNameList  List中
        for(TxtIndexset txtIndexset1 : indexsetEnglistNameList){
            String indexName = txtIndexset1.getEnglishName();
            qualityNameList.add(indexName);
        }
        System.out.println(qualityNameList);
        String tableName = txtDataset.getSurfaceName();
        TxtIndexset txtIndexset = new TxtIndexset();
        //list转字符串用  ， 分割
        String indexName = String.join("),MAX(", qualityNameList);
        String indexName2 = String.join("),MIN(", qualityNameList);
        StringBuffer sql = new StringBuffer();
        sql.append("select eigan_value,MAX(");
        sql.append(indexName);
        sql.append("),MIN(");
        sql.append(indexName2);
        sql.append(") from ");
        sql.append(tableName);
        sql.append(" where dataset_key = '");
        sql.append(datasetKey);
        sql.append(" '");
        sql.append(" group by eigan_value");
        List<LinkedHashMap<String, Object>> datasetList = txtGatherMapper.selectDatasetList(sql.toString());

        for (LinkedHashMap linkedHashMap : datasetList) {
            for(TxtIndexset txtIndexset1 : indexsetEnglistNameList){
                 indexName = txtIndexset1.getEnglishName();
                 String maxIndexName = "MAX("+indexName+")";
                String minIndexName = "MIN("+indexName+")";
                Double intMax = Double.valueOf(linkedHashMap.get(maxIndexName).toString());
                Double intMin = Double.valueOf(linkedHashMap.get(minIndexName).toString());
                TxtGather txtGather = new TxtGather();
                txtGather.setGatherKey(UUID.randomUUID().toString());
                //修改代码将字段中保存索引主键2020/8/21
                txtGather.setIndexName(txtIndexset1.getIndexsetKey());
                txtGather.setDeleteFlag(0);
                txtGather.setCreatBy(ShiroUtils.getUserId().toString());
                txtGather.setCreatTime(DateUtils.getNowDate());
                txtGather.setIndexMax(intMax);
                txtGather.setIndexMin(intMin);
                txtGather.setDatasetKey(datasetKey);
                if (null == linkedHashMap.get("eigan_value")) {
                    linkedHashMap.put("eigan_value", "1");
                }
                txtGather.setEigenvalue(linkedHashMap.get("eigan_value").toString());
                result = txtGatherMapper.insertTxtGather(txtGather);
            }
        }
        return result;
    }

    /**
     * 删除汇总显示对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTxtGatherByIds(String ids)
    {
        return txtGatherMapper.deleteTxtGatherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除汇总显示信息
     *
     * @param gatherKey 汇总显示ID
     * @return 结果
     */
    public int deleteTxtGatherById(String gatherKey)
    {
        return txtGatherMapper.deleteTxtGatherById(gatherKey);
    }
}
